package org.example.controller;


import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import org.example.entity.CommonResult;
import org.example.entity.DeliveryAddress;
import org.example.service.DeliveryAdressService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RefreshScope
public class DeliveryAdressController {
    @Resource
    private DeliveryAdressService deliveryAdressService;
    //原接口 DeliveryAddressController/listDeliveryAddressByUserId
    @GetMapping("/DeliveryAddressController/listDeliveryAddressByUserId/{userId}")
    public CommonResult<List<DeliveryAddress>> listDeliveryAddressByUserId(@PathVariable("userId") String userId){
        List<DeliveryAddress> addrList = deliveryAdressService.findAllByUserId(userId);
        if(addrList!=null){
            return new CommonResult<>(200,"listDeliveryAddressByUserId查找成功",addrList);
        }else {
            return new CommonResult<>(404,"listDeliveryAddressByUserId查找为空",null);
        }
    }

    @GetMapping("/DeliveryAddressController/getDeliveryAddressById/{daId}")
    public CommonResult<DeliveryAddress> listDeliveryAddressById(@PathVariable("daId") Integer daId){
        DeliveryAddress da = deliveryAdressService.findByDaId(daId);
        if(da!=null){
            return new CommonResult<>(200,"getDeliveryAddressById查找成功",da);
        }else {
            return new CommonResult<>(404,"getDeliveryAddressById查找为空",null);
        }
    }

    @PostMapping("/DeliveryAddressController/addDeliveryAddress")
    public CommonResult<DeliveryAddress> addDeliveryAddress(
            @RequestParam("ctName") String ctName,
            @RequestParam("ctSex") Integer ctSex,
            @RequestParam("ctTel") String ctTel,
            @RequestParam("address") String address,
            @RequestParam("userId") String userId
            ){
        DeliveryAddress d = new DeliveryAddress();
        d.setAddress(address);
        d.setContactName(ctName);
        d.setContactSex(ctSex);
        d.setContactTel(ctTel);
        d.setUserId(userId);
        DeliveryAddress da = deliveryAdressService.addDeliveryAdress(d);
        if(da!=null){
            return new CommonResult<>(200,"addDeliveryAddress成功",da);
        }else {
            return new CommonResult<>(404,"addDeliveryAddress失败",null);
        }
    }

    @PostMapping("/DeliveryAddressController/updateDeliveryAddress")
    @Transactional
    public CommonResult<String> updateDeliveryAddress(
            @RequestParam("daId") Integer daId,
            @RequestParam("ctName") String ctName,
            @RequestParam("ctSex") Integer ctSex,
            @RequestParam("ctTel") String ctTel,
            @RequestParam("address") String address
    ){
        deliveryAdressService.updateByDaIdFromEntity(daId, address, ctTel, ctSex, ctName);
        return new CommonResult<>(200,"updateDeliveryAddress成功",null);
    }

    @GetMapping("/DeliveryAddressController/removeDeliveryAddress/{daId}")
    public CommonResult<String> deleteByDaId(@PathVariable("daId") Integer daId){
        deliveryAdressService.deleteByDaId(daId);
        return new CommonResult<>(200,"deleteByDaId成功",null);
    }

}
